#include <bits/stdc++.h>
// 2025/02/06
// tag: 
// Author: Zhang Muen
using namespace std;

using int64 = int64_t;

int n, cow[100001], st1[100001], st2[100001];

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> cow[i];
    int ans = 0, top1 = 0, top2 = 0;
    for (int i = 1; i <= n; i++){
        while (top1 && cow[st1[top1]] >= cow[i])
            top1--;
        while (top2 && cow[st2[top2]] < cow[i])
            top2--;
        int p = upper_bound(st1 + 1, st1 + 1 + top1, st2[top2]) - st1;
        if (p != top1 + 1)
            ans = max(ans, i - st1[p] + 1);
        st1[++top1] = i;
        st2[++top2] = i;
    }
    cout << ans;
    return 0;
}